package com.hncc.dms.service.impl;

import java.util.List;

import com.hncc.bms.domain.BmsBillsRecord;
import com.hncc.bms.domain.vo.BmsRecordListVo;
import com.hncc.bms.mapper.BmsBillsRecordMapper;
import com.hncc.dms.domain.DmsHerbalPrescriptionRecord;
import com.hncc.dms.domain.DmsMedicinePrescriptionRecord;
import com.hncc.dms.domain.DmsNonDrugItemRecord;
import com.hncc.dms.mapper.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.hncc.bms.mapper.BmsInvoiceRecordMapper;
import com.hncc.bms.domain.BmsInvoiceRecord;
import com.hncc.dms.service.IBmsInvoiceRecordService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 发票Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-09-04
 */
@Service
public class BmsInvoiceRecordServiceImpl implements IBmsInvoiceRecordService 
{
    @Resource
    private BmsInvoiceRecordMapper bmsInvoiceRecordMapper;

    @Resource
    private BmsBillsRecordMapper bmsBillsRecordMapper;

    @Resource
    private DmsNonDrugItemRecordMapper dmsNonDrugItemRecordMapper;

    @Resource
    private DmsMedicinePrescriptionRecordMapper dmsMedicinePrescriptionRecordMapper;

    @Resource
    private DmsHerbalPrescriptionRecordMapper dmsHerbalPrescriptionRecordMapper;

    /**
     * 查询发票
     * 
     * @param id 发票ID
     * @return 发票
     */
    @Override
    public BmsInvoiceRecord selectBmsInvoiceRecordById(Long id)
    {
        return bmsInvoiceRecordMapper.selectBmsInvoiceRecordById(id);
    }

    /**
     * 查询发票列表
     * 
     * @param bmsInvoiceRecord 发票
     * @return 发票
     */
    @Override
    public List<BmsInvoiceRecord> selectBmsInvoiceRecordList(BmsInvoiceRecord bmsInvoiceRecord)
    {
        return bmsInvoiceRecordMapper.selectBmsInvoiceRecordList(bmsInvoiceRecord);
    }

    /**
     * 新增发票
     * 
     * @param bmsInvoiceRecord 发票
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBmsInvoiceRecord(BmsInvoiceRecord bmsInvoiceRecord)
    {
        //根据挂号id查询流水表id
        BmsBillsRecord bmsBillsRecord = bmsBillsRecordMapper.selectBmsBillsRecordById(null,bmsInvoiceRecord.getRegistrationId());
        bmsInvoiceRecord.setBillId(bmsBillsRecord.getId());
        //改变缴费项目的状态
        updStatus(bmsInvoiceRecord.getBmsRecordListVos());
        //将缴费项目转为字符串
        bmsInvoiceRecord.setItemList(getRecord(bmsInvoiceRecord.getBmsRecordListVos(),bmsInvoiceRecord.getId()));
        bmsBillsRecord.setRecordList(bmsBillsRecord.getRecordList().concat(getRecord(bmsInvoiceRecord.getBmsRecordListVos(),bmsBillsRecord.getId())));
        //新增发票
        bmsInvoiceRecord.setCreateTime(DateUtils.getNowDate());
        int count = 0;
        count = bmsInvoiceRecordMapper.insertBmsInvoiceRecord(bmsInvoiceRecord);
        //根据流水账号Id查询发票数量
        bmsBillsRecord.setInvoiceNum(bmsInvoiceRecordMapper.selectCountInvoiceNumByBillId(bmsBillsRecord.getId()));
        //修改账单流水
        count = bmsBillsRecordMapper.updateBmsBillsRecord(bmsBillsRecord);
        return count;
    }

    /**
     * 修改缴费项目的状态
     * type 4:成药处方 5:草药处方 1:检查 2:检验 3:处置
     * @param bmsRecordListVos
     */
    @Transactional(rollbackFor = Exception.class)
    public void updStatus(List<BmsRecordListVo> bmsRecordListVos){
        for (int i = 0; i < bmsRecordListVos.size(); i++){
            BmsRecordListVo bmsRecordListVo = bmsRecordListVos.get(i);
            if(bmsRecordListVo.getType() == 5){
                DmsMedicinePrescriptionRecord dmsMedicinePrescriptionRecord = new DmsMedicinePrescriptionRecord();
                dmsMedicinePrescriptionRecord.setId(bmsRecordListVo.getId());
                if(bmsRecordListVo.getStatus() == 1){
                    dmsMedicinePrescriptionRecord.setStatus(2);
                }else{
                    dmsMedicinePrescriptionRecord.setStatus(0);
                }
                dmsMedicinePrescriptionRecordMapper.updateDmsMedicinePrescriptionRecord(dmsMedicinePrescriptionRecord);
            }else if(bmsRecordListVo.getType() == 4){
                DmsHerbalPrescriptionRecord dmsHerbalPrescriptionRecord = new DmsHerbalPrescriptionRecord();
                dmsHerbalPrescriptionRecord.setId(bmsRecordListVo.getId());
                if(bmsRecordListVo.getStatus() == 1){
                    dmsHerbalPrescriptionRecord.setStatus(2);
                }else{
                    dmsHerbalPrescriptionRecord.setStatus(0);
                }
                dmsHerbalPrescriptionRecordMapper.updateDmsHerbalPrescriptionRecord(dmsHerbalPrescriptionRecord);
            }else{
                DmsNonDrugItemRecord dmsNonDrugItemRecord = new DmsNonDrugItemRecord();
                dmsNonDrugItemRecord.setId(bmsRecordListVo.getId());
                if(bmsRecordListVo.getStatus() == 1){
                    dmsNonDrugItemRecord.setStatus(2);
                }else{
                    dmsNonDrugItemRecord.setStatus(5);
                }
                dmsNonDrugItemRecordMapper.updateDmsNonDrugItemRecord(dmsNonDrugItemRecord);
            }
        }
    }

    /**
     * 将缴费项目转为字符串
     * @param bmsRecordListVos 数据
     * @param id 判断是账单流水|发票
     * @return
     */
    public String getRecord(List<BmsRecordListVo> bmsRecordListVos,Long id){
        String list = "";
        System.out.println("list---size--->"+bmsRecordListVos.size());
        for (int i = 0; i < bmsRecordListVos.size(); i++){
            BmsRecordListVo bmsRecordListVo = bmsRecordListVos.get(i);
            if(id == null){
                list += bmsRecordListVo.getId()+","+bmsRecordListVo.getType()+","+bmsRecordListVo.getAmount()+"><";
            }else{
                list += bmsRecordListVo.getId()+","+bmsRecordListVo.getType()+"><";
            }
        }
        return list;
    }
    /**
     * 修改发票
     * 
     * @param bmsInvoiceRecord 发票
     * @return 结果
     */
    @Override
    public int updateBmsInvoiceRecord(BmsInvoiceRecord bmsInvoiceRecord)
    {
        return bmsInvoiceRecordMapper.updateBmsInvoiceRecord(bmsInvoiceRecord);
    }

    /**
     * 批量删除发票
     * 
     * @param ids 需要删除的发票ID
     * @return 结果
     */
    @Override
    public int deleteBmsInvoiceRecordByIds(Long[] ids)
    {
        return bmsInvoiceRecordMapper.deleteBmsInvoiceRecordByIds(ids);
    }

    /**
     * 删除发票信息
     * 
     * @param id 发票ID
     * @return 结果
     */
    @Override
    public int deleteBmsInvoiceRecordById(Long id)
    {
        return bmsInvoiceRecordMapper.deleteBmsInvoiceRecordById(id);
    }

    @Override
    public BmsInvoiceRecord getInvoiceNoByRegistrationId(Long registrationId) {
        return bmsInvoiceRecordMapper.selectInvoiceNoByRegistrationId(registrationId);
    }
}
